package app.tool;

import java.util.List;

import org.rygh.core.xml.XmlPath;

import wsi.depend.assist.Assistant;
import wsi.depend.assist.AssistantManager;
import wsi.function.Function;
import wsi.function.FunctionController;
import wsi.function.RuntimeInfo;
import wsi.script.AppConfig;
import wsi.task.Engine;
import wsi.ws.WebServices;
import app.App;

public class appendRerun extends App {

	public static void main(String[] args) {
		init();
		run();
		bye();
	}

	static void prepare() {
		AppConfig.__DEBUG_APPEND_RERUN = true;

		AssistantManager am = AssistantManager.shared();
		FunctionController fc = FunctionController.shared();

		Function f1, f2;

		f1 = fc.getFunction(WebServices.music_brainz.name(), "getReleasesByName");
		f2 = fc.getFunction(WebServices.music_brainz.name(), "getReleaseById");

		Assistant assi = new Assistant(f2, f1);
		assi.setXpath(new XmlPath("@id"));

		am.add(assi);
	}

	public static void run() {
		AppConfig.init();
		prepare();

		List<Function> functions = FunctionController.shared()
				.getAllFunctions();
		AssistantManager am = AssistantManager.shared();
		AppConfig config = AppConfig.shared();
		Engine engine = new Engine();

		for (Function function : functions) {
			if (!am.scheduled(function))
				continue;

			RuntimeInfo inputConfig = config.getConfig(function);

			if (inputConfig == null)
				continue;

			function.setup(inputConfig);
			function.getRuntimeInfo().setAssistNeeded(true);
			engine.process(function);
		}

		engine.save();
	}
}
